package com.luntan.school.util;


import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {
    /**
     * 将数据进行 MD5 加密，并以16进制字符串格式输出
     *
     * @param data 需要加密的数据
     * @return 加密后的16进制字符串
     */

    public static String md5(String data) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("md5");
            byte[] md5 = messageDigest.digest(data.getBytes(StandardCharsets.UTF_8));

            //将字节数据转换为十六进制
            for (byte b : md5) {
                stringBuilder.append(Integer.toHexString(b&0xff));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(Md5Util.md5("123456"));
    }
}
